<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Models\UserModel;
use Encore\Admin\Show;

class UserController extends BaseController
{
    protected $title = '用户管理';

    /**
     * grid
     * @return NGrid
     */
    protected function grid(): NGrid
    {
        $grid = new NGrid(new UserModel);
        $grid->model()->leftJoin('user_dm', 'user_dm.user_id', 'user.id')
            ->leftJoin('user_system', 'user_system.user_id', 'user.id')
            ->where('user_type_id', 2)->select(['user_dm.*', 'user.*', 'user_system.id as system'])
            ->orderByDesc('user.id');
        //$grid->expandFilter();
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('user-id', __('ID'))->integer();
                $filter->equal('mobile', __('com.mobile'))->integer();
                $filter->in('auth_status', __('user.auth_status'))->select(__('user.authStatusArr'));
                $filter->equal('is_seller', __('user.is_seller'))->select(__('com.whether'));
                $filter->where(function ($query) {
                    if ((int)$this->input === 0) $query->whereNull('unionid');
                    else $query->whereNotNull('unionid');
                }, __('com.unionid'))->select(__('com.whether'));
                $filter->equal('wifi_auto_play', __('user.wifi_auto_play'))->select(__('com.whether'));
                $filter->where(function ($query) {
                    $query->where('user.created_at', '>=', $this->input);
                }, __('com.created_at') . '>=')->datetime();
                $filter->where(function ($query) {
                    if ((int)$this->input === 0) $query->whereNull('user_system.id');
                    else $query->whereNotNull('user_system.id');
                }, __('user.system'))->select(__('com.whether'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('serial_number', __('com.serial_number'));
                $filter->like('nickname', __('user.nickname'));
                $filter->in('status', __('user.status'))->multipleSelect(__('user.statusArr'));
                $filter->where(function ($query) {
                    if ((int)$this->input === 0) $query->whereNull('mp_openid');
                    else $query->whereNotNull('mp_openid');
                }, __('com.mp_openid'))->select(__('com.whether'));
                $filter->equal('targeted_push', __('user.targeted_push'))->select(__('com.whether'));
                $filter->equal('gender', __('com.gender'))->select(__('com.genderArr'));
                $filter->where(function ($query) {
                    $query->where('user.created_at', '<=', $this->input);
                }, __('com.created_at') . '<=')->datetime();
            });
        });
        $grid->column('id', __('ID'))->sortable();
        $grid->column('serial_number', __('com.serial_number'));
        $grid->column('mobile', __('com.mobile'));
        $grid->column('avatar_file_id', __('user.avatar_file_id'))->display(function ($field) {
            return fileIdToUrl($field);
        })->image(config('oss.url'), 50, 50);
        $grid->column('system', __('user.system'))->display(function ($field) {
            return is_null($field) ? '否' : '是';
        });
        $grid->column('nickname', __('user.nickname'));
        $grid->column('gender', __('com.gender'))->using(__('com.genderArr'));
        $grid->column('auth_status', __('user.auth_status'))->using(__('user.authStatusArr'));
        $grid->column('is_seller', __('user.is_seller'))->using(__('com.whether'));
        $grid->column('unionid', __('com.unionid'))->display(function ($field) {
            return __('com.whether')[(int)!is_null($field)];
        });
        $grid->column('mp_openid', __('com.mp_openid'))->display(function ($field) {
            return __('com.whether')[(int)!is_null($field)];
        });
        $grid->column('wifi_auto_play', __('user.wifi_auto_play'))->using(__('com.whether'));
        $grid->column('targeted_push', __('user.targeted_push'))->using(__('com.whether'));
        $grid->column('status', __('user.status'))->using(__('user.statusArr'));
        $grid->column('created_at', __('com.created_at'));
        return $grid;
    }

    /**
     * detail
     * @param int $id
     * @return Show
     */
    protected function detail(int $id): Show
    {
        $show = new Show(UserModel::query()->leftJoin('user_dm', 'user_dm.user_id', 'user.id')
            ->leftJoin('user_system', 'user_system.user_id', 'user.id')
            ->where('user_type_id', 2)->where('user.id', $id)->first(['user_dm.*', 'user.*', 'user_system.id as system']));
        $show->field('id', __('ID'));
        $show->field('serial_number', __('com.serial_number'));
        $show->field('mobile', __('com.mobile'));
        $show->field('avatar_file_id', __('user.avatar_file_id'))->as(function ($field) {
            return fileIdToUrl($field);
        })->image(config('oss.url'), 100, 100);
        $show->field('nickname', __('user.nickname'));
        $show->field('gender', __('com.gender'))->using(__('com.genderArr'));
        $show->field('auth_status', __('user.auth_status'))->using(__('user.authStatusArr'));
        $show->field('is_seller', __('user.is_seller'))->using(__('com.whether'));
        $show->field('unionid', __('com.unionid'))->as(function ($field) {
            return __('com.whether')[(int)!is_null($field)];
        });
        $show->field('mp_openid', __('com.mp_openid'))->as(function ($field) {
            return __('com.whether')[(int)!is_null($field)];
        });
        $show->field('wifi_auto_play', __('user.wifi_auto_play'))->using(__('com.whether'));
        $show->field('targeted_push', __('user.targeted_push'))->using(__('com.whether'));
        $show->field('status', __('user.status'))->using(__('user.statusArr'));
        $show->field('invite_count', __('user.invite_count'));
        $show->field('invite_sale', __('user.invite_sale'));
        $show->field('wx_nickname', __('user.wx_nickname'));
        $show->field('birthday', __('user.birthday'));
        $show->field('province', __('com.province'));
        $show->field('city', __('com.city'));
        $show->field('district', __('com.district'));
        $show->field('street', __('com.street'));
        $show->field('signature', __('com.signature'));
        $show->field('id_num', __('com.id_num'));
        $show->field('auth_file_id', __('com.auth_file_id'));
        $show->field('auth_file_id', __('com.auth_file_id'))->as(function ($field) {
            return fileIdToUrl($field);
        })->image(config('oss.url'), 100);
        $show->field('auth_reverse_file_id', __('com.auth_reverse_file_id'))->as(function ($field) {
            return fileIdToUrl($field);
        })->image(config('oss.url'), 100);
        $show->field('name', __('user.name'));
        $show->field('level', __('user.level'));
        $show->field('refund_times', __('user.refund_times'));
        $show->field('follow_count', __('user.follow_count'));
        $show->field('collect_count', __('user.collect_count'));
        $show->field('coupon_count', __('user.coupon_count'));
        $show->field('first_login_at', __('user.first_login_at'));
        $show->field('created_at', __('com.created_at'));
        $show->field('system', __('user.system'))->as(function ($field) {
            return is_null($field) ? '否' : '是';
        });
        $show->field('info', __('user.info'))->unescape()->as(function ($field) {
            return encode($field);
        });
        return $show;
    }
}
